package com.kodin.webview;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SDCardUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.kodin.polyfit.CurveFit;
import com.kodin.polyfit.PointFitView;
import com.kodin.polyfit.ProbItem;
import com.kodin.polyfit.Result;

import java.util.Map;

public class SplashActivity extends Activity {
    PointFitView point_fit;
    TextView log_fit, input_log;
    int guessNum = 9, step = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.polyfit_layout);
        point_fit = findViewById(R.id.point_fit);
        input_log = findViewById(R.id.input_log);
        log_fit = findViewById(R.id.log_fit);
        log_fit.setMovementMethod(new ScrollingMovementMethod());
        Spinner spinner_step = findViewById(R.id.spinner_step);
        spinner_step.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                step = Integer.parseInt((String) spinner_step.getAdapter().getItem(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        Spinner spinner_num = findViewById(R.id.spinner_num);
        spinner_num.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                guessNum = Integer.parseInt((String) spinner_num.getAdapter().getItem(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        String path = SDCardUtils.getSDCardPathByEnvironment() + "/yp.json";
        String json = FileIOUtils.readFile2String(path);
        ProbItem probItem = GsonUtils.fromJson(json, ProbItem.class);
        int len = probItem.getJ7().length-1;
        xx = new double[len];
        yy = new double[len];
        for (int index = 0; index < probItem.getJ7().length-1; index++) {
            if(index==0){
                xx[0]=0;
                yy[0]=0;
            }else {
                xx[index]=probItem.getJ7()[index+1];
                yy[index]=probItem.getY5()[index-1];
            }
        }
    }

    //    public static double[] xx = {20, 151, 271, 365, 497, 587, 687, 863, 947, 1047, 1116, 1114, 1144, 1261, 1383, 1476, 1626, 1706, 1780, 1844, 1891, 1949, 1999, 2046, 2089, 2127, 2174, 2242, 2287, 2347, 2387, 2421, 2452, 2488, 2520, 2539, 2562, 2609, 2648, 2676};
//    public static double[] yy = {0, 40, 117, 171, 280, 370, 470, 680, 780, 940, 1010, 1020, 1080, 1300, 1550, 1780, 2250, 2520, 2800, 3050, 3300, 3530, 3810, 4100, 4380, 4630, 5000, 5530, 6020, 6550, 7030, 7570, 8050, 8540, 9100, 9470, 10000, 11020, 12250, 13270};
//    public static double[] xx = {20, 151, 271, 497, 587, 687, 863, 947, 1047, 1116, 1114, 1144, 1261, 1383, 1476, 1626, 1706, 1780, 1844, 1891, 1949, 1999, 2046, 2089, 2127, 2174, 2242, 2287, 2347, 2387, 2421, 2452, 2488, 2520, 2539, 2562, 2609, 2648, 2676};
//    public static double[] yy = {0, 40, 117, 280, 370, 470, 680, 780, 940, 1010, 1020, 1080, 1300, 1550, 1780, 2250, 2520, 2800, 3050, 3300, 3530, 3810, 4100, 4380, 4630, 5000, 5530, 6020, 6550, 7030, 7570, 8050, 8540, 9100, 9470, 10000, 11020, 12250, 13270};
    public static double[] xx = {0, 231, 400, 632, 905, 1208, 1782, 2215, 2593, 2731};
    public static double[] yy = {0, 46, 116, 280, 560, 1010, 2520, 4990, 10000, 14990};

    public static double[][] fitSrc = new double[xx.length][2];

    public void polyFitY(View view) {
        EditText input_xx = findViewById(R.id.input_xx);
        float xx = Float.parseFloat(input_xx.getText().toString());
        input_log.setText("" + CurveFit.fitRealByBest(result, xx));
    }

    double[] result;

    public void polyFit(View view) {
        view.setClickable(false);
        log_fit.setText("计算中");
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<String>() {
            @Override
            public String doInBackground() throws Throwable {
                long start = System.currentTimeMillis();
                int all = xx.length;
                for (int index = 0; index < all; index++) {
                    fitSrc[index][0] = xx[index];
                    fitSrc[index][1] = yy[index];
                }
                LogUtils.e(fitSrc.length, fitSrc[0].length);
                Map<String, Object> re = CurveFit.curveFit(fitSrc, guessNum + 1);
                result = (double[]) re.get("best");
                if (result == null) return "模拟失败!";
                int len = (int) (xx[xx.length - 1] + 1);
                double[][] fitSrcTemp2 = new double[len][2];
                for (int index = 0; index < len; index++) {
                    fitSrcTemp2[index][0] = index;
                    fitSrcTemp2[index][1] = CurveFit.fitRealByBest(result, index);
                }
                point_fit.setData(fitSrcTemp2, fitSrc);
                return (System.currentTimeMillis() - start) + (String) re.get("log");
            }

            @Override
            public void onSuccess(String result) {
                log_fit.setText(result);
                view.setClickable(true);
            }
        });
    }

    private void poly() {
        int st = 4;
        int all = xx.length;
        int size = st + all / step + 1;
        for (int index = 0; index < all; index++) {
            fitSrc[index][0] = xx[index];
            fitSrc[index][1] = yy[index];
        }
        double[][] fitSrcTemp = new double[size][2];
        for (int index = 0; index < fitSrc.length; index++) {
            double[] item = fitSrc[index];
            if (index < st) {
                fitSrcTemp[index] = item;
            } else if (index == fitSrc.length - 1) {
                fitSrcTemp[size - 1] = fitSrc[index];
            } else {
                int stepT = index / step;
                if (index % step != 0) continue;
                if ((stepT + st) >= size) break;
                fitSrcTemp[stepT + st] = item;
            }
        }
        LogUtils.e(fitSrcTemp.length, fitSrcTemp[0].length);
        LogUtils.e(fitSrc.length, fitSrc[0].length);
        Map<String, Object> re = CurveFit.curveFit(fitSrc, guessNum + 1);
        result = (double[]) re.get("best");
        int len = (int) (xx[xx.length - 1] + 1);
        double[][] fitSrcTemp2 = new double[len][2];
        for (int index = 0; index < len; index++) {
            fitSrcTemp2[index][0] = index;
            fitSrcTemp2[index][1] = CurveFit.fitRealByBest(result, index);
        }
        point_fit.setData(fitSrcTemp2, fitSrc);
    }
}
